// 每四个字符空格vin码用
// 获取状态栏高度、底部虚拟按键高度
// 传入'top'或'bottom'
import DeviceInfo from 'react-native-device-info';
// import ExtraDimensions from 'react-native-extra-dimensions-android';
import { Platform, StatusBar, NativeModules } from 'react-native';
import Constant from '../utils/Constant';
// import HttpUtils from '../common/HttpUtils';

let deviceName = DeviceInfo.getModel();
let deviceId = DeviceInfo.getDeviceId();

const { StatusBarManager } = NativeModules;

const isIos = Platform.OS == 'ios';
export function AddSpace(val) {
    if (val) {
        let comtext = val.replace(/\s/g, ' ').replace(/[^\da-zA-Z]/g, ' ').replace(/([\da-zA-Z]{4})(?=[\da-zA-Z])/g, '$1  ');
        return comtext;
    }

}
export function trim(val) {
    if (val) {
        let trimstr = val.replace(/\s/ig, '');
        return trimstr;
    }
}


export function formatResult(result) {

    return result ? result : '';

}

export function getImgUrl(imgUrl) {
    if (imgUrl == undefined) {
        return ''
    }
    if (!imgUrl || imgUrl == '') {
        return ''
    }
    if (imgUrl.startsWith('http')) {

        return imgUrl
    } else {

        if (imgUrl.startsWith('/')) {
            let temp = imgUrl.replace('/', '')
            let str = Constant.imgDomain + temp

            return str
        } else {
            return Constant.imgDomain + imgUrl
        }

    }
}

export function getWebUrl(webUrl) {
    if (webUrl == undefined) {
        return ''
    }
    if (!webUrl || webUrl == '') {
        return ''
    }
    if (webUrl.startsWith('http')) {

        return webUrl
    } else {

        if (webUrl.startsWith('/')) {
            let temp = webUrl.replace('/', '')
            let str = Constant.webDomain + temp

            return str
        } else {
            return Constant.webDomain + imgUrl
        }

    }
}

/**
 * 
 * @param {*} imgUrl 
 * @param {*} type  1返回公司logo默认图 2返回车辆默认图 
 * @returns 
 */
export function getImgUrlByType(imgUrl, type) {

    if (imgUrl == undefined || !imgUrl || imgUrl == '') {
        if (type == 1) {
            return isIos ? 'com_74' : 'default_company_logo_round'
        } else {
            return ''
        }

    }

    if (imgUrl.startsWith('http')) {
        return imgUrl
    } else {
        return Constant.imgDomain + imgUrl
    }
}

export function getOpenImImgUrl(imgUrl) {
    if (imgUrl == undefined) {
        return ''
    }
    if (!imgUrl || imgUrl == '') {
        return ''
    }
    if (imgUrl.startsWith('http')) {
        return imgUrl
    } else {
        return 'https://scim.shuaiche.com/' + imgUrl
    }
}


// 货币格式转换
export function Fmoney(s) {
    s = (s + "").replace(/[^\d\.-]/g, "");
    var l = s.split(".")[0].split("").reverse();
    let t = "";
    for (let i = 0; i < l.length; i++) {
        t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
    }
    return t.split("").reverse().join("");
}

export function changeTwoDecimal_f(x) {
    try {
        let f_x1 = parseFloat(x);
        if (isNaN(f_x1)) {
            return x;
        }
        let f_x = Math.round(x * 100) / 100 / 10000;

        var s = f_x + "";
        var str = s.substring(0, s.indexOf(".") + 3);
        console.log('str = ' + str);
        return str;
    } catch (e) {
        return '0.00';
    }
}

//检测jsBundle版本
export function getCarRegisterDate(str) {
    if (isEmpty(str)) {
        return "";
    } else {
        return str.length > 7 ? str.substring(0, 7) : str;
    }
}

export function getMileFormat(str) {
    if (str == null || str == 0) {
        return "";
    } else {
        return (str / 10000).toFixed(2) + '万公里'
    }
}

export function getPriceFormat(str) {
    if (str == null || str == 0 || str == 'null') {
        return "未定价";
    } else {
        return (str / 10000).toFixed(2) + '万元'
    }
}

export function priceFenToYuanFormat(str) {
    if (str == null || str == 0 || str == 'null') {
        return "未定价";
    } else {
        str = str / 100 / 10000;
        return str.toFixed(2) + '万元'
    }
}


// 严格判空（含空格）
function isEmpty(str) {
    return str == null || str.trim() === "";
}


export function getStoreStatus(storeStatus) {
    if (storeStatus == 1) {
        return "在途";
    } else if (storeStatus == 2) {
        return "在厅";
    } else if (storeStatus == 3) {
        return "整备";
    } else if (storeStatus == 4) {
        return "外借";
    } else if (storeStatus == 5) {
        return "在途";
    } else if (storeStatus == 6) {
        return "在途";
    }
}

//1:预售,2:在售,3:已预订,4:已售,5:下架	
export function getSaleStatus(saleStatus) {
    if (saleStatus == 1) {
        return "预售";
    } else if (saleStatus == 2) {
        return "在售";
    } else if (saleStatus == 3) {
        return "已预订";
    } else if (saleStatus == 4) {
        return "已售";
    } else if (saleStatus == 5) {
        return "下架";
    } else if (saleStatus == 6) {
        return "退库";
    }
}

export function getCheckStatus(detectionQuotationStatus) {
    if (detectionQuotationStatus == 1) {
        return "未自检";
    } else if (detectionQuotationStatus == 2) {
        return "待自检";
    } else if (detectionQuotationStatus == 3) {
        return "已自检";
    } else if (detectionQuotationStatus == 4) {//加盟商视角
        return "已报价";
    }
    return ''
}

export function getPurchaseType(purchaseType) {
    if (purchaseType == 1) {
        return "全款";
    } else if (purchaseType == 2) {
        return "合作";
    } else if (purchaseType == 3) {
        return "寄售";
    }
    return ''
}

export function formatTimestamp(timestamp) {
    const now = new Date();
    const targetDate = new Date(timestamp);
    const diff = now - targetDate;


    const diffMinutes = Math.floor(diff / (1000 * 60));
    const diffHours = Math.floor(diff / (1000 * 60 * 60));
    const diffDays = Math.floor(diff / (1000 * 60 * 60 * 24));

    if (diffMinutes < 60) {
        return `${diffMinutes} 分钟前`;
    } else if (diffHours < 24) {
        return `${diffHours} 小时前`;
    } else {
        const year = targetDate.getFullYear();
        const month = String(targetDate.getMonth() + 1).padStart(2, '0');
        const day = String(targetDate.getDate()).padStart(2, '0');
        return `${year}-${month}-${day}`;
    }
}

export function getMsgContentShow(msgInfo) {
    if (msgInfo.contentType == 101) {//文本消息
        return msgInfo.senderName + '：' + JSON.parse(msgInfo.content).content
    } else if (msgInfo.contentType == 102) {//图片消息

        return msgInfo.senderName + '：' + '[图片]'
    } else if (msgInfo.contentType == 103) {//语音消息
        return msgInfo.senderName + '：' + '[语音]' + JSON.parse(msgInfo.content).duration + "\""
    } else if (msgInfo.contentType == 104) {//视频消息
        return msgInfo.senderName + '：' + '[视频]'
    }
    else if (msgInfo.contentType == 105) {//文件消息
        return msgInfo.senderName + '：' + '[文件]' + JSON.parse(msgInfo.content).fileName
    }else if(msgInfo.contentType == 1501){//群创建通知
        return '帅车-客服机器人' + '：' + '可以开始聊天了'
    }else if (msgInfo.contentType == 1509) {//邀请群成员通知
        
        let contentInfo = JSON.parse(msgInfo.content)
        let detailInfo = JSON.parse(contentInfo.detail)
        console.log('detailInfo',detailInfo)
        if(detailInfo.invitedUserList && detailInfo.invitedUserList.length > 0){
            return '帅车-客服机器人' + '：' + detailInfo.invitedUserList[0].nickname+'进入群聊'
        }
        
        return '帅车-客服机器人' + '：' + '新成员进群通知'
    }

    else if (msgInfo.contentType == 110) {//自定义消息
        let contentInfo = JSON.parse(msgInfo.content)
        if (contentInfo.data == '1') {//车辆
            return msgInfo.senderName + '：' + '[车辆]'
        } else if (contentInfo.data == '2') {//订单
            return msgInfo.senderName + '：' + '[订单]'
        } else if (contentInfo.data == '3') {//报价订单
            return msgInfo.senderName + '：' + '[报价单]'
        } else if (contentInfo.data == '4') {//询价单
            return msgInfo.senderName + '：' + '[订单消息]'
        }else if (contentInfo.data == '5') {//售后单
            return msgInfo.senderName + '：' + '[售后订单消息]'
        }
        else if (contentInfo.data == '6') {//纯文案
            let itemInfo = JSON.parse(contentInfo.extension)
            let title = ''
            if (itemInfo.userTitle && itemInfo.userTitle.length > 0) {
                itemInfo.userTitle.map((item, index) => {
                    if (item.merchantId == Constant.userInfo.merchantId) {
                        title = item.title
                    }
                })
                if (title == '') {
                    title = itemInfo.title
                }
            } else {
                title = itemInfo.title
            }
            return msgInfo.senderName + '：' + title
        }else if(contentInfo.data == '7'){
            return '车辆推荐消息'
        }
        else {
            return '其他消息'
        }

    }
    else {
        return '其他消息'
    }
}

export function getPerm(perm) {
    if (Constant.userInfo.perms == undefined) {
        return Number(0)
    }
    console.log('Constant.userInfo.perms',Constant.userInfo.perms)
    const perms = BigInt(Constant.userInfo.perms);
    const result = perms & BigInt(perm); // 使用 BigInt 的按位与
    console.log('getPermresult', result)
    return Number(result)
}



